<template>
  <div>
    <el-tabs v-model="activeName" @tab-click="getPageList">
      <el-tab-pane label="未开始" name="1"></el-tab-pane>
      <el-tab-pane label="进行中" name="2"></el-tab-pane>
      <el-tab-pane label="已结束" name="3"></el-tab-pane>
    </el-tabs>
    <!-- 搜索框 -->
    <el-row>
      <el-col :span="5">
        <el-select v-model="selectValue" placeholder="请选择搜索方式">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="10">
        <el-input v-if="selectValue=='选项1'||selectValue=='选项2'" v-model="searchContent" :placeholder="searchPlaceWord" prefix-icon="el-icon-search"></el-input>
        <div class="block" v-else-if="selectValue=='选项3'">
          <el-date-picker
            v-model="searchDate"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            value-format="yyyy-MM-dd hh:mm:ss">
          </el-date-picker>
        </div>
      </el-col>
      <el-col :span="9">
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-col>
    </el-row>

    <div style="margin-top: 20px">
      <!-- 考试信息表 -->
      <el-table style="width: 100%" border :data="list"
        :row-class-name="tableRowClassName"
        :header-cell-style="{background:'#F2F6FC',color:'#606266'}"
      >
        <el-table-column prop="name" label="考试名称" width="width">
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="100">
        </el-table-column>
        <el-table-column prop="prop" label="人脸需求" width="80" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.isFaceCheck? 'success' : 'danger'"
              disable-transitions>{{scope.row.isFaceCheck?'√':'×'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="prop" label="提前交卷" width="80" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.isAdvance? 'success' : 'danger'"
              disable-transitions>{{scope.row.isAdvance?'√':'×'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="prop" label="允许进入考试" width="75" align="center">
          <template slot-scope="scope">
            <el-tag
              type="warning"
              disable-transitions>{{scope.row.timeLimit}} min
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="lastTime" label="总时长" width="80" align="center">
          <template slot-scope="scope">
            <el-tag
              type="primary"
              disable-transitions>{{scope.row.lastTime}} min</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="开始时间" width="100" align="center">
        </el-table-column>
        <el-table-column prop="endTime" label="结束时间" width="100" align="center">
        </el-table-column>
        <el-table-column prop="prop" label="操作" width="150" align="center">
          <template slot-scope="{row,$index}">
            <el-tooltip class="item" effect="dark" content="报名" placement="top">
              <el-button v-if="activeName=='1'" type="primary" icon="el-icon-key" circle @click="signUp(row)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="考试相关信息" placement="top">
              <el-button type="info" icon="el-icon-info" circle @click="showDialog(row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        style="margin-top: 20px; text-align: center"
        :current-page="page"
        :total="total"
        :page-size="limit"
        :page-count="7"
        :page-sizes="[3, 5, 10]"
        @current-change="getQuestionList"
        @size-change="handleSizeChange"
        layout="prev, pager, next, jumper, ->, sizes, total"
      >
      </el-pagination>
    </div>
    <!-- 考试的详情 -->
    <el-dialog title="考试详情" :visible.sync="detailVisible" width="350px">
      <!-- 考试的详情框 -->
      <el-descriptions>
        <el-descriptions-item >考试名称: {{detailList.name}}</el-descriptions-item><br><br>
        <el-descriptions-item >备注: {{detailList.remark}}</el-descriptions-item><br><br>
        <el-descriptions-item >
          是否需要打开摄像头: 
          <el-tag
            :type="detailList.isFaceCheck? 'success' : 'danger'"
            disable-transitions>{{detailList.isFaceCheck?'√':'×'}}
          </el-tag>
        </el-descriptions-item><br>
        <el-descriptions-item >
          是否能提前交卷: 
          <el-tag
            :type="detailList.isAdvance? 'success' : 'danger'"
            disable-transitions>{{detailList.isAdvance?'√':'×'}}
          </el-tag>
        </el-descriptions-item><br>
        <el-descriptions-item >
          是否支持复制粘贴: 
          <el-tag
            :type="detailList.isCopyPaste? 'success' : 'danger'"
            disable-transitions>{{detailList.isCopyPaste?'√':'×'}}
          </el-tag>
        </el-descriptions-item><br>
        <el-descriptions-item >
          开始考试后能够进入考场时间: 
          <el-tag
            type="warning"
            disable-transitions>{{detailList.timeLimit}} min
          </el-tag>
        </el-descriptions-item><br>
        <el-descriptions-item >
          考试总时长: 
          <el-tag
            type="primary"
            disable-transitions>{{detailList.lastTime}} min
          </el-tag>
        </el-descriptions-item><br><br>
        <el-descriptions-item >考试开始时间: {{detailList.startTime}}</el-descriptions-item><br><br>
        <el-descriptions-item >考试结束时间: {{detailList.endTime}}</el-descriptions-item><br><br>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name:'examination_registration',
  data() {
    return {
      //当前看中状态
      activeName: '1',
      //代表的分页器第几页
      page:1,
      //当前页数展示数据条数
      limit:3,
      //总共数据条数
      total:0,
      //处于该状态的试卷列表
      list:[],
      //关键词搜索考试
      searchContent:'',
      //exambody条件
      examForm:{
        id:'',
        //试卷id
        paperId:'',
        //考试名称
        name:'',
        //备注
        remark:'',
        //是否在考试中开启人脸认证
        isFaceCheck:'',
        //限时进入（分钟）
        timeLimit:'',
        //是否可以提前交卷
        isAdvance:'',
        //是否支持复制黏贴
        isCopyPaste:'',
        //开始时间
        startTime:'',
        //结束时间
        endTime:'',
        //持续时间（分钟）
        lastTime:'',
        //创建时间
        gmtCreate:'',
        //修改时间
        gmtModified:'',
      },
      //用于搜索
      options: [{
        value: '选项1',
        label: '考试名称'
      }, {
        value: '选项2',
        label: '考试备注'
      }, {
        value: '选项3',
        label: '日期'
      }, ],
      //下拉框默认值
      selectValue: '选项1',
      //输入框占位符内容
      searchPlaceWord:'请输入考试名称',
      // 快捷搜索日期选项
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 搜索的日期内容
      searchDate: '',
      // 考试详情弹窗的显示
      detailVisible:false,
      // 考试详情弹窗显示的列表内容
      detailList:[],
    }
  },
  mounted(){
    this.getPageList();
  },
  methods: {
    // 报名考试
    async signUp(row){
      //弹框
      this.$confirm(`确定报名该考试吗?------ ${row.name}:${row.remark}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(async() => {
        //当用户点击确定按钮时会触发
        //向服务器发送请求
        //调用报名接口
          let result = await this.$API.examination_registration.registerForExam(row.id);
          //如果删除成功
          if(result.code==20000){
            this.$message({
              type: 'success',
              message: '报名成功!'
            });
            //再次获取考试列表
            this.getPageList();
          }
        }).catch(() => {
        //当用户点击取消按钮时会触发
          this.$message({
            type: 'info',
            message: '已取消报名'
          });          
      });
    },
    // 展示考试详情弹窗
    showDialog(row){
      this.detailList = row;
      this.detailVisible = true;
    },
    //展示列表
    async getPageList(){
      let result = await this.$API.examination_registration.reqGetExamInfoByState(this.examForm,this.page,this.limit,parseInt(this.activeName));
      if(result.code==20000){
        this.list = result.data.exams;
        this.total = result.data.total;
      }
    },
    //当分页器某一页需要展示数据条数变化时触发
    handleSizeChange(limit){
      this.limit = limit;
      this.getPageList();
    },
    tableRowClassName({row, rowIndex}) {
      if (rowIndex%2 === 1) {
        return 'success-row';
      }
      return '';
    },
    //搜索
    search(){
      //先判断一下有无搜索内容
      //根据搜索类型，赋值
      if(this.selectValue=='选项1'){
        this.examForm.name = this.searchContent;
      }else if(this.selectValue=='选项2'){
        this.examForm.remark = this.searchContent;
      }else if(this.selectValue=='选项3'){
        if(this.searchDate){
          this.examForm.startTime = this.searchDate[0];
          this.examForm.endTime = this.searchDate[1];
        }else{
          this.examForm.startTime = '';
          this.examForm.endTime = '';
        }
      }
      //重新调用获取列表函数
      this.getPageList();
    },
  },
  watch:{
    //监听selectValue
    selectValue:{
      handler(newValue,oldValue){
        // 应该会清空输入框数据
        this.searchContent = '';
        // 想要根据下拉列表变化输入框占位符
        if(newValue=='选项1'){
          this.searchPlaceWord = '请输入考试名称';
        }else if(newValue=='选项2'){
          this.searchPlaceWord = '请输入考试备注信息';
        }
      }
    },
    //监听activeName，只要切换了状态，输入框内容应该为空
    activeName:{
      handler(){
        this.searchContent = '';
        this.selectValue = '选项1';
      }
    },
  },
}
</script>

<style>
.el-table .success-row {
  background: #F2F6FC;
}
</style>